local skels = {}

Fk:addQmlMark{
    name = "ym1__zhuiji",
    how_to_show = function (name, value, player)
        local mark = player:getTableMark("ym1__zhuiji")
        local strs = {"","","判","摸","出","弃"}
        local str = ""
        for i = 3, 6, 1 do
            if not table.contains(mark, i) then
                str = str .. strs[i]
            end
        end
        return str
    end,
    qml_path = " ",
}
local zhuiji = fk.CreateSkill{
    name = "ym1__zhuiji",
}
Fk:loadTranslationTable{
    ["ym1__zhuiji"] = "追姬",
    [":ym1__zhuiji"] = "每项限一次，一名角色的①判定②摸牌③出牌④弃牌阶段结束时，若此阶段你的手牌数或体力值变化为本回合首次，你可令一名角色回复1点体力或摸两张牌。若仅剩一项，此技能无次数与条件限制，但仅能于你的回合发动。",
    ["#ym1__zhuiji"] = "是否发动 追姬，令一名角色回复1点体力或摸两张牌？",
    ["ym1__zhuiji_active"] = "追姬",
    ["@[ym1__zhuiji]"] = "追姬",

    ["$ym1__zhuiji1"] = "不过是些微代价罢了。",
    ["$ym1__zhuiji2"] = "哼，以为这就能难倒我吗？",
}
zhuiji:addEffect(fk.EventPhaseEnd, {
    can_trigger = function (self, event, target, player, data)
        if player:hasSkill(zhuiji.name) and target and target.phase > Player.Start and target.phase < Player.Finish then
            local mark = player:getTableMark(zhuiji.name)
            if not table.contains(mark, target.phase) then
                if #mark == 3 and target == player then
                    return true
                else
                    local room = player.room
                    local turnCardEvent = room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
                        for _, move in ipairs(e.data) do
                            if move.to == player and move.toArea == Card.PlayerHand then
                                return true
                            end
                            if move.from == player then
                                for _, info in ipairs(move.moveInfo) do
                                    if info.fromArea == Card.PlayerHand then
                                        return true
                                    end
                                end
                            end
                        end
                    end, Player.HistoryTurn)
                    local phaseCardEvent = room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
                        for _, move in ipairs(e.data) do
                            if move.to == player and move.toArea == Card.PlayerHand then
                                return true
                            end
                            if move.from == player then
                                for _, info in ipairs(move.moveInfo) do
                                    if info.fromArea == Card.PlayerHand then
                                        return true
                                    end
                                end
                            end
                        end
                    end, Player.HistoryPhase)
                    local turnHpEvent = room.logic:getEventsOfScope(GameEvent.ChangeHp, 1, function (e)
                        if e.data and e.data.who == player then
                            return true
                        end
                    end, Player.HistoryTurn)
                    local phaseHpEvent = room.logic:getEventsOfScope(GameEvent.ChangeHp, 1, function (e)
                        if e.data and e.data.who == player then
                            return true
                        end
                    end, Player.HistoryPhase)
                    return (#phaseCardEvent > 0 and turnCardEvent[1].id == phaseCardEvent[1].id) or (#phaseHpEvent > 0 and phaseHpEvent[1].id == turnHpEvent[1].id)
                end
            end
        end
    end,
    on_cost = function (self, event, target, player, data)
        local bool, dat = player.room:askToUseActiveSkill(player, {
            skill_name = "ym1__zhuiji_active",
            prompt = "#ym1__zhuiji"
        })
        if bool and dat then
            local anim_type = "drawcard"
            if dat.interaction == "recover" then
                anim_type = "support"
            end
            event:setCostData(self, { tos = dat.targets, choice = dat.interaction, anim_type = anim_type })
            return true
        end
    end,
    on_use = function (self, event, target, player, data)
        local room = player.room
        local to = event:getCostData(self).tos[1]
        local choice = event:getCostData(self).choice
        local mark = player:getTableMark(zhuiji.name)
        if #mark < 3 then
            room:addTableMarkIfNeed(player, zhuiji.name, target.phase)
        end
        if choice == "recover" then
            room:recover{
                who = to,
                num = 1,
                skillName = zhuiji.name,
                recoverBy = player,
            }
        else
            to:drawCards(2, zhuiji.name)
        end
    end,
})
zhuiji:addAcquireEffect(function (self, player, is_start)
    player.room:setPlayerMark(player, "@[ym1__zhuiji]", 1)
end)
zhuiji:addLoseEffect(function (self, player, is_death)
    player.room:setPlayerMark(player, "@[ym1__zhuiji]", 0)
end)
table.insert(skels, zhuiji)

local zhuiji_active = fk.CreateSkill{
    name = "ym1__zhuiji_active"
}
zhuiji_active:addEffect("active", {
    card_num = 0,
    card_filter = Util.FalseFunc,
    target_num = 1,
    target_filter = function (self, player, to_select, selected, selected_cards, card, extra_data)
        if self.interaction.data and #selected == 0 then
            if self.interaction.data == "recover" then
                return to_select:isWounded()
            else
                return true
            end
        end
    end,
    interaction = function (self, player)
        return UI.ComboBox{ choices = {"recover","draw2"} }
    end,
})
table.insert(skels, zhuiji_active)

return skels